import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
class MyThreadPool{
    //.创建任务队列
    private BlockingQueue<Runnable> blockingQueue=null;
    //.创建固定线程数目的线程池
    public MyThreadPool(int n) throws InterruptedException {
        //给任务队列给出容量大小
        blockingQueue=new ArrayBlockingQueue<>(1000);
        //创建线程
      for(int i=0;i<n;i++){
          Thread t=new Thread(()->{
              Runnable task= null;
              try {
                  while(true){
                      task = blockingQueue.take();
                      task.run();
                  }

              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }


          });
//          t.setDaemon(true);
          t.start();

      }
    }
    //提交任务,将任务放入任务队列
    public void submit(Runnable task) throws InterruptedException {
        blockingQueue.put(task);
    }
}

public class demo40 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool=new MyThreadPool(10);
        //往线程池里面提交任务
        for(int i=0;i<100;i++){
            int id=i;
            myThreadPool.submit(()->{
                System.out.println(Thread.currentThread().getName()+" "+id);
            });
        }

    }
}
